package commonclass;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author liweimin
 * @date 2021/3/25 19:43
 * @description java 时间 API使用
 */
public class DateTest {
    @Test
    public void beforeJava8() {
        //自1970-01-01 00:00:00到现在的毫秒数
        long l = System.currentTimeMillis();
        // this(System.currentTimeMillis())
        Date date = new Date();
        long time = date.getTime();
        System.out.println(date.toString());
        System.out.println(date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        System.out.println(simpleDateFormat.format(date));
        System.out.println(new SimpleDateFormat("yyyy年-MM月-dd日 HH时:mm分:ss秒").format(date));
        Date parse;
        try {
            parse = simpleDateFormat.parse("2021年-03月-27日 09时:07分:58秒");
            System.out.println(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //日历抽象类--->子类GregorianCalendar
        Calendar instance = Calendar.getInstance();
        System.out.println(instance);
        Date time1 = instance.getTime();
        Date date1 = new Date(234234235235L);
        instance.setTime(date1);
        instance.set(Calendar.DAY_OF_MONTH, 8);
        System.out.println(instance.getTime());
        instance.set(Calendar.MINUTE, 40);
        System.out.println(instance.getTime());
        instance.add(Calendar.YEAR, 40);
        System.out.println(instance.getTime());
        Calendar instance1 = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINESE);
        System.out.println(instance1);
    }

    @Test
    public void afterJava8() {
        //日期
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        //时间
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);
        //日期+时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        LocalDateTime dateTime = LocalDateTime.of(2021, 3, 27, 10, 57);
        dateTime.getDayOfMonth();
        dateTime.getMonth();
        System.out.println(dateTime.getDayOfWeek());
        LocalDateTime localDateTime1 = dateTime.plusDays(1);
        System.out.println(localDateTime1);
    }

    @Test
    public void afterJava8_1() {
        System.out.println(Instant.now());
        //时间戳
        System.out.println(Instant.now().toEpochMilli());
        //1970-01-01 加上多少毫秒
        System.out.println(Instant.ofEpochMilli(12L));
    }

    @Test
    public void afterJava8_2() {
        //时间格式化
        DateTimeFormatter isoLocalDate = DateTimeFormatter.ISO_LOCAL_DATE;
        System.out.println(isoLocalDate.format(LocalDate.now()));
    }

    @Test
    public void afterJava8_3() {
        //时区
        System.out.println(ZoneId.systemDefault());
        System.out.println(ZoneId.SHORT_IDS);
        System.out.println(ZonedDateTime.now());
    }

    @Test
    public void afterJava8_4() {
        Clock clock = Clock.systemUTC();
        System.out.println(clock);
        System.out.println(clock.instant());
    }

    @Test
    public void afterJava8_5() {
        //计算间隔:日期 Period
        LocalDate now = LocalDate.now();
        LocalDate localDate = LocalDate.of(1010, 12, 12);
        Period between = Period.between(localDate, now);
        System.out.println(between.toString());
        System.out.println(between.getDays());
    }

    @Test
    public void afterJava8_6() {
        //计算间隔：时间 Duration
        LocalTime now = LocalTime.now();
        LocalTime localTime = LocalTime.of(7, 2, 3);
        Duration between = Duration.between(localTime, now);
        System.out.println(between);
        System.out.println(between.getSeconds());

        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime dateTime = LocalDateTime.of(2020, 1, 2, 8, 30);
        Duration duration = Duration.between(dateTime, localDateTime);
        System.out.println(duration);
        System.out.println(duration.getSeconds());
    }

    @Test
    public void afterJava8_7() {
        //时间校正器---->下个周五
        TemporalAdjuster next = TemporalAdjusters.next(DayOfWeek.FRIDAY);
        LocalDateTime with = LocalDateTime.now().with(next);
        System.out.println(with.format(DateTimeFormatter.ISO_DATE));
        //下一个工作日
        LocalDate time = LocalDate.now().with(temporal -> {
            LocalDate date = (LocalDate) temporal;
            if (date.getDayOfWeek().equals(DayOfWeek.FRIDAY)) {
                return date.plusDays(3);
            } else if (date.getDayOfWeek().equals(DayOfWeek.SATURDAY)) {
                return date.plusDays(2);
            } else {
                return date.plusDays(1);
            }
        });
        System.out.println(time.format(DateTimeFormatter.ISO_DATE));
    }
}
